package cn.iocoder.yudao.module.booking.controller.app.courseinfo;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.booking.controller.admin.courseinfo.vo.CourseInfoPageReqVO;
import cn.iocoder.yudao.module.booking.controller.admin.courseinfo.vo.CourseInfoRespVO;
import cn.iocoder.yudao.module.booking.controller.admin.courseinfo.vo.CourseInfoSaveReqVO;
import cn.iocoder.yudao.module.booking.controller.app.courseinfo.vo.AppCourseInfoRespVO;
import cn.iocoder.yudao.module.booking.dal.dataobject.courseinfo.CourseInfoDO;
import cn.iocoder.yudao.module.booking.service.courseinfo.CourseInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 课程信息")
@RestController
@RequestMapping("/booking/course-info")
@Validated
public class AppCourseInfoController {

    @Resource
    private CourseInfoService courseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建课程信息")
    public CommonResult<Long> createCourseInfo(@Valid @RequestBody CourseInfoSaveReqVO createReqVO) {
        return success(courseInfoService.createCourseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程信息")
    public CommonResult<Boolean> updateCourseInfo(@Valid @RequestBody CourseInfoSaveReqVO updateReqVO) {
        courseInfoService.updateCourseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCourseInfo(@RequestParam("id") Long id) {
        courseInfoService.deleteCourseInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCourseInfoRespVO> getCourseInfo(@RequestParam("id") Long id) {
        CourseInfoDO courseInfo = courseInfoService.getCourseInfo(id);
        return success(BeanUtils.toBean(courseInfo, AppCourseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程信息分页")
    public CommonResult<PageResult<AppCourseInfoRespVO>> getCourseInfoPage(@Valid CourseInfoPageReqVO pageReqVO) {
        PageResult<CourseInfoDO> pageResult = courseInfoService.getCourseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppCourseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程信息 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseInfoExcel(@Valid CourseInfoPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseInfoDO> list = courseInfoService.getCourseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程信息.xls", "数据", CourseInfoRespVO.class,
                BeanUtils.toBean(list, CourseInfoRespVO.class));
    }

}